const { getForumList, queryForumList, removeForum, getForumDetails, getComment, getAllComment, delComment, publishComment } = require('../models/forumModel')

module.exports = {
  // 获取用户列表数据
  getForumList: async (ctx) => {
    // 获取用户列表数据
    let res = await getForumList();

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无用户",
        data: []
      }
    }
  },
  // 查询论坛
  queryForumList: async (ctx) => {
    const { username, title } = ctx.request.query;
    // 查询论坛
    let res = await queryForumList(username, title);

    if (res.length === 0) {
      return ctx.body = {
        status: 205,
        msg: "此用户没有发表或无此标题",
        data: []
      }
    }
    ctx.body = {
      status: 200,
      msg: "查询成功",
      data: res
    }
  },
  // 删除论坛
  removeForum: async (ctx) => {
    const { id } = ctx.request.body;

    // 删除论坛
    let res = await removeForum(id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "删除成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "删除失败"
      }
    }
  },
  // 获取论坛详情数据
  getForumDetails: async (ctx) => {
    const { id } = ctx.request.query;
    // 获取论坛详情数据
    let res = await getForumDetails(id);

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res[0]
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
      }
    }
  },
  // 获取论坛评论数据
  getComment: async (ctx) => {
    const { id, current, pagesize } = ctx.request.query;
    // 获取论坛评论数据
    let res = await getComment(id, current, pagesize);
    // 获取全部评论数据
    let all = await getAllComment(id);

    if (res.length !== 0) {
      ctx.body = {
        status: 200,
        msg: "获取成功",
        data: res,
        total: all.length
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "暂无数据",
        data: [],
        total: 0,
        current: current,
        pages: current - 1
      }
    }
  },
  // 删除评论
  delComment: async (ctx) => {
    const { id } = ctx.request.body;
    // 删除评论
    let res = await delComment(id);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "删除成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "删除失败"
      }
    }
  },
  // 发表评论
  publishComment: async (ctx) => {
    const { forum_id, user_id, username, avatar, content, createtime } = ctx.request.body;
    let res = await publishComment(forum_id, user_id, username, avatar, content, createtime);

    if (res.affectedRows === 1) {
      ctx.body = {
        status: 200,
        msg: "发表成功"
      }
    } else {
      ctx.body = {
        status: 205,
        msg: "发表失败"
      }
    }
  }
}